import 'package:aichat_flutter_wallet/module_common/provider/nav_key.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/dialog/browser_inside_dialog.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/dialog/browser_loading_dialog.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/public_tip/public_tip.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DialogUtils {
  static void showBrowserLoading(BuildContext context) {
    showDialog(
      barrierColor: Colors.transparent,
      barrierDismissible: true,
      context: context,
      builder: (BuildContext context) {
        return BrowserLoadingDialog();
      },
    );
  }

  static Future<String?> showBrowserInside({required BuildContext context, required String url,bool canCallBack=false}) {
   return showModalBottomSheet(
      isDismissible: false,
      context: context,
      isScrollControlled: true,
      enableDrag: false,
      backgroundColor: Colors.transparent,
      constraints:
          BoxConstraints(maxHeight: MediaQuery.of(context).size.height - MediaQuery.of(context).viewPadding.top),
      builder: (BuildContext context) {
        return BrowserInsideDialog(
          url: url,
          canCallBack: canCallBack,
        );
      },
    );
  }

  // 显示弹窗
  static Future showDialogModal(
      {BuildContext? context, required Widget child, Color barrierColor = Colors.black45}) async {
    return showDialog(
      barrierColor: barrierColor,
      barrierDismissible: true,
      context: context ?? NavKey.getContext(),
      builder: (BuildContext context) {
        return child;
      },
    );
  }

  // toast
  static showToast(
      {BuildContext? context,
      required String message,
      int duration = 3000,
      String position = 'bottom',
      String status = "0"}) async {
    BuildContext curContext = context ?? NavKey.getContext();
    double positionY = 0;
    if (position == 'top') {
      positionY = -300;
    } else if (position == 'center') {
      positionY = -0;
    } else {
      positionY = 200;
    }
    showDialog(
      barrierColor: Colors.transparent,
      barrierDismissible: false,
      context: curContext,
      builder: (BuildContext context) {
        return Transform.translate(
          offset: Offset(0, positionY.w),
          child: ToastContent(
            message: message,
            status: status,
          ),
        );
      },
    );
    Future.delayed(Duration(milliseconds: duration), () {
      Navigator.of(curContext).pop();
    });
  }
}
